package com.vsd.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.demo.dialet.PaginationSupport;
import com.demo.util.SqlUtils;
import com.vsd.daoEx.model.AreaAgentEx;
import com.vsd.domain.AreaAgent;
import com.vsd.repository.AreaAgentRepository;
import com.vsd.service.AreaAgentService;

@Service
@Transactional
public class AreaAgentServiceImpl implements AreaAgentService {

	@SuppressWarnings("unused")
	private static final Logger LOGGER = LoggerFactory
			.getLogger(AreaAgentServiceImpl.class);
	
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	@Autowired
	private AreaAgentRepository areaAgentRepository;

	@Override
	public List<AreaAgentEx> selectExList() {
		// TODO Auto-generated method stub
		String sql = "select a.*,c.name as cityName from c_area_agent a left join f_city c on a.area_code = c.code where 1=1 order by a.create_time desc";
		
		sql = PaginationSupport.initPage(sql, jdbcTemplate);
		
		List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
		List<AreaAgentEx> ret = new ArrayList<>();
		for (Map<String, Object> map : maps) {
			AreaAgentEx ex = new AreaAgentEx();
			SqlUtils.transMap2Bean2(map, ex);
			ret.add(ex);
		}
		return ret;
	}

	@Override
	public int updateByPrimaryKeySelective(AreaAgent agent) {
		// TODO Auto-generated method stub
		try {
			String sql = SqlUtils.update(agent, true, " where id = '"+agent.getId()+"'");
			return jdbcTemplate.update(sql);
		}catch (Exception e) {
			return 0;
		}
	}

	@Override
	public void save(AreaAgent a) {
		// TODO Auto-generated method stub
		areaAgentRepository.save(a);
	}

	@Override
	public void delById(String id) {
		// TODO Auto-generated method stub
		areaAgentRepository.delete(id);
	}

	@Override
	public AreaAgent findOne(String id) {
		// TODO Auto-generated method stub
		return areaAgentRepository.findOne(id);
	}

	@Override
	public AreaAgent findByAccount(String account) {
		// TODO Auto-generated method stub
		Collection<AreaAgent> list = areaAgentRepository.findByAccount(account);
		if(list.size()>0) return list.iterator().next();
		return null;
	}

	@Override
	public AreaAgent findByAccountAndPassword(String account, String password) {
		// TODO Auto-generated method stub
		Collection<AreaAgent> list = areaAgentRepository.findByAccountAndPassword(account, password);
		if(list.size()>0) return list.iterator().next();
		return null;
	}

	
}
